package com.hb.game.pk.ui.activity.mine.recommend;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.hb.game.pk.R;
import com.hb.game.pk.adpter.RecommendActivityListAdapter;
import com.hb.game.pk.api.Host;
import com.hb.game.pk.api.RedBag;
import com.hb.game.pk.base.BaseActivity;
import com.hb.game.pk.beans.SYbean;
import com.hb.game.pk.network.RetrofitCreateHelper;
import com.hb.game.pk.utils.SpannableStringUtils;
import com.hb.game.pk.utils.ToastUtils;
import com.hb.game.pk.widgets.titlebar.TitleBar;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class RecommendActivity extends BaseActivity<SYbean> {
    @BindView(R.id.title_bar)
    TitleBar titleBar;
    @BindView(R.id.tv_ssfy)
    TextView tvSsfy;
    @BindView(R.id.tv_gtfy)
    TextView tvGtfy;
    @BindView(R.id.tv_total)
    TextView tvTotal;
    @BindView(R.id.tv_today)
    TextView tvToday;
    List<SYbean.DataBean.ListBean> list;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_recommend;
    }

    @Override
    protected int getRootViewId() {
        return R.id.layout_root;
    }

    @Override
    protected int getRefreshId() {
        return 0;
    }

    @Override
    protected int getListViewId() {
        return R.id.rv_list;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        checkedType(tvSsfy, tvGtfy);
        titleBar.getLlLeft().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RecommendActivity.this.finish();
            }
        });
        titleBar.getLlRight().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.putExtra("counts", 0);
                startActivity(RecommendListActivity.class);
            }
        });
        tvTotal.setText(SpannableStringUtils.getBuilder("收益总金额")
                .setForegroundColor(getResources().getColor(R.color.c_000000))
                .append("0.00").setForegroundColor(getResources().getColor(R.color.c_536eff))
                .append("元").setForegroundColor(getResources().getColor(R.color.c_000000))
                .create());
        tvToday.setText(SpannableStringUtils.getBuilder("今天收益")
                .setForegroundColor(getResources().getColor(R.color.c_000000))
                .append("0.00").setForegroundColor(getResources().getColor(R.color.c_536eff))
                .append("元").setForegroundColor(getResources().getColor(R.color.c_000000))
                .create());
    }

    private void checkedType(TextView checked, TextView noChecked) {
        checked.setTextColor(getResources().getColor(R.color.c_536eff));
        checked.setBackgroundResource(R.drawable.login_type_checked_list);
        noChecked.setBackgroundDrawable(null);
        noChecked.setTextColor(getResources().getColor(R.color.c_000000));
    }

    @OnClick({R.id.tv_ssfy, R.id.tv_gtfy})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_ssfy:
                checkedType(tvSsfy, tvGtfy);
                break;
            case R.id.tv_gtfy:
                checkedType(tvGtfy, tvSsfy);
                break;
        }
    }

    @Override
    protected void onRefreshData() {
        super.onRefreshData();
        call = RetrofitCreateHelper.createApi(RedBag.class, Host.HOST).tj();
        call.enqueue(new Callback<SYbean>() {
            @Override
            public void onResponse(Call<SYbean> call, Response<SYbean> response) {
                SYbean body = response.body();
                if (body == null) {
                    ToastUtils.showToast("请求错误");
                    return;
                }
                if (body.getStatus() != 1) {
                    ToastUtils.showToast(body.getInfo());
                    return;
                }
                SYbean.DataBean data = body.getData();
                String tmoney = "0";
                if (data.getSum_money() == null) {
                    tmoney = "0";
                } else {
                    tmoney = Float.parseFloat(data.getSum_money()) / 100f + "";
                }
                tvToday.setText(SpannableStringUtils.getBuilder("收益总金额")
                        .setForegroundColor(getResources().getColor(R.color.c_000000))
                        .append(tmoney).setForegroundColor(getResources().getColor(R.color.c_536eff))
                        .append("元").setForegroundColor(getResources().getColor(R.color.c_000000))
                        .create());
                String money = "0";
                if (data.getMoney() == null) {
                    money = "0";
                } else {
                    money = Float.parseFloat(data.getMoney()) / 100f + "";
                }
                tvTotal.setText(SpannableStringUtils.getBuilder("今天收益")
                        .setForegroundColor(getResources().getColor(R.color.c_000000))
                        .append(money).setForegroundColor(getResources().getColor(R.color.c_536eff))
                        .append("元").setForegroundColor(getResources().getColor(R.color.c_000000))
                        .create());
                list = data.getList();
                initAdapter();
            }

            @Override
            public void onFailure(Call<SYbean> call, Throwable t) {
                ToastUtils.showToast("网络请求错误");
            }
        });
    }

    @Override
    protected void initAdapter() {
        if (mAdapter == null) {
            mAdapter = new RecommendActivityListAdapter(R.layout.item_recommend_activity_list);
            mAdapter.setNewData(list);
        } else {
            mAdapter.setNewData(list);
        }
        super.initAdapter();
    }

    @Override
    public boolean isNeedLoadMore() {
        return false;
    }
}
